/**
 * 2011-08-23 用户角色并联
 * huhui
 */



import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;
var selectOrg:String = "";
private function initApp():void
{
	remoteObject.orgtree();
	remoteObject.getUserRole(this.userid);
}

//获取所选择的用户的组织id
public	function getUserInfoHd(event:ResultEvent):void
{
	var userMsg:Object = event.result;
	
	if(userMsg!=null&&userMsg.cuserId!=null)
	{
		for(var i:int = 0;i<userMsg.orgs.length;i++){
			selectOrg+=userMsg.orgs[i].corgId+","
			
		}
	}
}

private function orgtreeHandler(event:ResultEvent):void
{
	orgtreeac = event.result as ArrayCollection;
	orgtree.dataProvider = orgtreeac;
	orgtree.labelField="organizationName";
}



private function TreeChange():void {


	var orgid:String = orgtree.selectedItem.id;
	//如果用户属于此组织
	if(selectOrg.indexOf(orgid)!=-1){
		remoteObject.getorg_role(orgid);
	}else{
		Alert.show("该用户不属于选择的组织，不能关联角色。");
		return;
	}
}  


private function getorg_roleHandler(event:ResultEvent):void
{

	orgrleac = event.result as ArrayCollection;
	datagrid.dataProvider = orgrleac;
}

public  function userRoleAdd(obj:Object):void
{	
	for(var i:int=0;i<userRoleac.length;i++)
	{
		var o:Object = userRoleac.getItemAt(i);
		if(o.croleId==obj.croleId)
		{
			Alert.show("该角色已经与该用户并联！","提示");
			return;
		}
	}
	userRoleac.addItem(obj);
	datagrid1.dataProvider=userRoleac;	
}

public  function deluserRole(id:String):void
{	
	for(var i:int=0;i<userRoleac.length;i++)
	{
		var o:Object = userRoleac.getItemAt(i);
		if(o.croleId==id)
		{
			userRoleac.removeItemAt(i);
		}
	}
}

public function saveUserRole()
{
	var roleidArr:Array = new Array();
	for(var i:int=0;i<userRoleac.length;i++)
	{
		var o:Object = userRoleac.getItemAt(i);
		roleidArr.push(o.croleId);
	}
	remoteObject.saveUserRole(this.userid,roleidArr);
	
}
private function saveUserRoleHandler(event:ResultEvent):void
{
	if(event.result.state)
	{
		Alert.show(event.result.msg,"提示");
	}
}
private function getUserRoleHandler(event:ResultEvent):void
{
	userRoleac = event.result as ArrayCollection;
	datagrid1.dataProvider=userRoleac;	
}
/*
private function faultHandler(event:FaultEvent):void{
	Alert.show("出错："+event.fault.faultString);
}
*/